const service = require('../service/categoryService');
const router = require('koa-router')();

router.prefix('/api/wx/category');

/**
 * @api {post} /api/wx/category/getCategory 获取类目列表
 * @apiName getCategory
 * @apiGroup category
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：获取失败，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Objext[]} data 获取成功时返回的类目数据
 * @apiSuccess {String} data.category_title 类目标题
 * @apiSuccess {String} data.category_content 类目内容
 * @apiSuccess {String} picture 主图url
 * @apiSuccess {Objext[]} caseData 获取成功时返回的推荐案例数据
 * @apiSuccess {String} caseData.case_id 案例主键值
 * @apiSuccess {String} caseData.case_name 案例名称
 * @apiSuccess {String} caseData.case_mainImg 图片
 */
router.post('/getCategory', async (ctx) => {
    try {
        let result, result1, result2;
        result = await service.handleGetCategory();
        result1 = await service.handleGetImgUrl();
        result2 = await service.handleGetCase();
        if (result1.code === 1) {
            result.picture_url = result1.data[0].picture_url;
        } else {
            result.picture_url = '';
        }
        if (result2.code === 1) {
            result.caseData = result2.data;
        } else {
            result.caseData = [];
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});
module.exports = router;